package com.letcoding.spiderAutoHome.job;

import com.letcoding.spiderAutoHome.api.AutoHomeApiService;
import com.letcoding.spiderAutoHome.module.CarTest;
import com.letcoding.spiderAutoHome.service.CarTestService;
import com.letcoding.spiderAutoHome.util.TitleFilter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CrawlerAutoHomeJob
 * @Description TODO
 * @Author will
 * @Date 2021/5/9 20:18
 */
@Slf4j
@DisallowConcurrentExecution
public class CrawlerAutoHomeJob  extends QuartzJobBean {

    private AutoHomeApiService autoHomeApiService;
    private TitleFilter titleFilter;
    private CarTestService carTestService;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>> start crawlerAutoHomeJob");
        // 获取spring容器
        ApplicationContext applicationContext = (ApplicationContext) context.getJobDetail().getJobDataMap()
                .get("context");

        // 获取抓取数据服务
        this.autoHomeApiService = applicationContext.getBean(AutoHomeApiService.class);
        // 获取汽车评测服务
        this.carTestService = applicationContext.getBean(CarTestService.class);
        // 获取过滤器
        this.titleFilter = applicationContext.getBean(TitleFilter.class);

        List<CarTest> saveList = new ArrayList<>();
        for (int i = 1; i < 188; i++) {
            String baseUrl = "https://www.autohome.com.cn/bestauto/" + i;
            String html = autoHomeApiService.getHtml(baseUrl);
            Document document = Jsoup.parse(html);
            Elements carElements = document.getElementsByClass("uibox");
            for (Element carElement : carElements) {
                String carTitle = carElement.getElementsByClass("uibox-title uibox-title-border").text();
               /* if (titleFilter.contains(carTitle)) {
                    continue;
                }*/
                CarTest carTest = marshalCarElement(carElement);
                String imageNames = marshalImageNames(carElement);
                carTest.setImage(imageNames);
                saveList.add(carTest);
            }
            if (!CollectionUtils.isEmpty(saveList)) {
                carTestService.saveBatch(saveList);
            }
        }
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>> end crawlerAutoHomeJob");
    }

    /**
     * 解析数据下载评测图片
     * @param carElement
     * @return
     */

    private String marshalImageNames(Element carElement) {
        String carImageName = null;
        List<String> imageNameList = new ArrayList<>();
        Elements imageElements = carElement.select(".piclist-box.fn-clear ul.piclist02 a");
        for (Element imageElement : imageElements) {
            String imageUrl = "https:" + imageElement.getElementsByTag("img").attr("src");
            String imageName = autoHomeApiService.getImage(imageUrl);
            imageNameList.add(imageName);
        }
        if (!CollectionUtils.isEmpty(imageNameList)) {
            carImageName =  StringUtils.join(imageNameList, ",");
        }
        return carImageName;
    }

    /**
     * 解析数据封装成汽车评测对象
     * @param carElement
     * @return
     */
    private CarTest marshalCarElement(Element carElement) {
        CarTest carTest = new CarTest();
        String carTitle = carElement.getElementsByClass("uibox-title uibox-title-border").text();
        carTest.setTitle(carTitle);
        String testSpeed = carElement.select(".tabbox1 dd:nth-child(2) > div.dd-div2").first().text();
        carTest.setTestSpeed(covertStrToNum(testSpeed));
        String testBrake = carElement.select(".tabbox1 dd:nth-child(3) > div.dd-div2").first().text();
        carTest.setTestBrake(covertStrToNum(testBrake));
        String testOil = carElement.select(".tabbox1 dd:nth-child(4) > div.dd-div2").first().text();
        carTest.setTestOil(covertStrToNum(testOil));
        String editorName1 = carElement.select(".tabbox2.tabbox-score dd:nth-child(2) > div.dd-div1").first().text();
        carTest.setEditorName1(editorName1);
        String editorRemark1 = carElement.select(".tabbox2.tabbox-score dd:nth-child(2) > div.dd-div3").first().text();
        carTest.setEditorRemark1(editorRemark1);
        String editorName2 = carElement.select(".tabbox2.tabbox-score dd:nth-child(3) > div.dd-div1").first().text();
        carTest.setEditorName2(editorName2);
        String editorRemark2 = carElement.select(".tabbox2.tabbox-score dd:nth-child(3) > div.dd-div3").first().text();
        carTest.setEditorRemark2(editorRemark2);
        String editorName3 = carElement.select(".tabbox2.tabbox-score dd:nth-child(4) > div.dd-div1").first().text();
        carTest.setEditorName3(editorName3);
        String editorRemark3 = carElement.select(".tabbox2.tabbox-score dd:nth-child(4) > div.dd-div3").first().text();
        carTest.setEditorRemark3(editorRemark3);
        Date currentDate = new Date();
        carTest.setCreated(currentDate);
        carTest.setUpdated(currentDate);
        return carTest;
    }


    /**
     * 把字符串去掉最后一个数，转为乘以1000的数字
     * @param str
     * @return
     */
    private int covertStrToNum(String str) {
        try {
            if ("--".equals(str)) {
                return 0;
            }
            // 字符串去掉随后一个数
            str = StringUtils.substring(str, 0, str.length() - 1);
            // 转换为小数并乘以1000
            Number num = Float.valueOf(str) * 1000;
            return num.intValue();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(str);
        }
        return 0;
    }
}
